package ge.telasi.tasks.ui.comp;

import java.awt.FontMetrics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JLabel;
/**
 * @author dimitri
 */
public class ResizableLabel extends JLabel {
    private String fullText;

    public ResizableLabel() {
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                reset();
            }
        });
    }

    public void setFullText(String fullText) {
        this.fullText = fullText;
        reset();
    }

    public String getFullText() {
        return fullText;
    }

    private void reset() {
        FontMetrics fm = getFontMetrics(getFont());
        String text = fullText;
        if (fm != null && text != null) {
            int width = getSize().width;
            while (fm.stringWidth(text) > width) {
                if (text.length() < 4) {
                    break;
                }
                text = text.substring(0, text.length() - 4) + "...";
            }
        }
        setText(text);
    }


}
